<template>
  <div class="page-login">
    <div class="login-content">
      <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      size="medium"
    >
    <h3>账号登录</h3>
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即登录</el-button
        >
      </el-form-item>
    </el-form>
    </div>
    
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度在 1 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          this.$H
            .post("/1.1/login", this.ruleForm)
            .then((res) => {
              console.log(res);
              let {username,sessionToken} = res.data
              this.$store.commit("user/saveToken", {username,sessionToken});
              this.$message({
                message: "登录成功",
                type: "success",
              });
              this.$router.push("/");
            })
            .catch((err) => {this.$message({
                message: "账号或密码输入错误",
                type: "fail",
              });});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.page-login {
  height: 100vh;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  position: relative;
  background-image: url('http://lc-hyBuikQD.cn-n1.lcfile.com/8aVtILsA8zO0TMDnXwIayKiDvuGHVogA/7.png');
  background-size: cover;

  h3{
    text-align: center;
    color: #fff;
  }
  .login-content{
    position: absolute;
    left: 66%;
    top: 50%;
    transform: translate(0,-50%);
    padding: 20px 30px 20px 20px;
    border-radius: 10px;
   background-color: rgb(34, 40, 50);
  }
}
/deep/.el-form-item__content {
  width: 300px;
  margin-left: 60px !important;
}
/deep/.el-form-item__label{
  color: #fff;
  width: 60px !important;
}
/deep/.el-button{
  width: 300px;
}
/deep/.el-input__inner{
  color: #fff;
  background-color: transparent;
}
/deep/.el-button--primary{
  background-color: transparent;
  border-color: none;
}
</style>